package com.my.juc.completable;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/3/6 17:15
 */
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 异步调用 没有返回值
        /*CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "completableFuture");
        });
        
        cf.get();*/
                
        
        
        // 异步调用 有返回值
        CompletableFuture<Integer> cf2 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "completableFuture2");   
            // 模拟异常
            int i = 1 / 0;
            return 1024;
        });
        
        
        
        cf2.whenComplete((t, u) -> {
            System.out.println("----t=" + t); // 方法返回值
            System.out.println("----u=" + u); // 方法中异常的信息
        }).get();
    }
    
}
